package tests.kr.pe.mkseo.data;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import junit.framework.Assert;
import junit.framework.TestCase;
import kr.pe.mkseo.data.Account;
import kr.pe.mkseo.data.AllAccounts;
import kr.pe.mkseo.data.Category;
import kr.pe.mkseo.data.Record;

import com.google.common.collect.Iterables;

public class AllAccountsTest extends TestCase {
	
	private Account fund_accnt1;
	private Account fund_accnt2;
	private Account bank_accnt1;
	private AllAccounts all_;
	private Category fund_category;
	private Category bank_category;
	
	public void setUp() {
		fund_category = Category.CreateCategory("Fund", "My fund");
		bank_category = Category.CreateCategory("Bank", "My bank category");
		
		fund_accnt1 = new Account("f1", null);
		fund_accnt1.AddBalanceRecord(new Record(Calendar.getInstance(), 
												           new BigDecimal("100")));
		fund_accnt1.AddBalanceRecord(new Record(Calendar.getInstance(), 
				new BigDecimal("150")));
		
		fund_accnt2 = new Account("f2", null);
		fund_accnt2.AddBalanceRecord(new Record(Calendar.getInstance(), 
				new BigDecimal("50")));
		
		bank_accnt1 = new Account("b1", null);
	  bank_accnt1.AddBalanceRecord(new Record(Calendar.getInstance(), 
				new BigDecimal("20")));
	  
		all_ = new AllAccounts();
		all_.Add(fund_category, fund_accnt1);
		all_.Add(fund_category, fund_accnt2);
		all_.Add(bank_category, bank_accnt1);
	}
	
	/**
	 * Tests if adding an account works correctly.
	 */
	public void testAddAccount() {
		
		List<Account> funds = all_.Get(fund_category);
		Assert.assertEquals(2, funds.size());
		Assert.assertTrue(Iterables.contains(funds, fund_accnt1));
		Assert.assertTrue(Iterables.contains(funds, fund_accnt2));
		
		List<Account> banks = all_.Get(bank_category);
		Assert.assertEquals(1, banks.size());
		Assert.assertTrue(Iterables.contains(banks, bank_accnt1));
	}
	
	/**
	 * Tests if retrieving account for a category retrieves accounts in the
	 * specified order.
	 */
	public void testAccountNameSort() {
		Assert.assertTrue(true);
	}
	
	/**
	 * Tests if sum for a account category is correct.
	 */
	public void testAmountSum() {
		Assert.assertEquals(new BigDecimal("200"),
				all_.GetAccountSum(fund_category));
		Assert.assertEquals(new BigDecimal("20"), 
				all_.GetAccountSum(bank_category));
	}
}